package com.meihua.shellapp.fragment.achievement;

import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.webview.WebActivity;
import com.meihua.shellapp.base.BaseFragment;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.AchievementBean;
import com.meihua.shellapp.databean.AchievementDataBean;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;

import butterknife.Bind;

/**
 * 作者：lc on 2017/9/26.
 * 邮箱：lc0607live@163.com
 */

public class AchievementSelectFragment extends BaseFragment {

    public int type;
    private AchievementDataBean data;

    @Bind(R.id.rl_month)
    RelativeLayout rl_month;//当月
    @Bind(R.id.tv_month_money)
    TextView tv_month_money;//当月钱
    @Bind(R.id.rl_quarter)
    RelativeLayout rl_quarter;//当季
    @Bind(R.id.tv_quarter_money)
    TextView tv_quarter_money;//当季钱
    @Bind(R.id.rl_halfyear)
    RelativeLayout rl_halfyear;//半年
    @Bind(R.id.tv_halfyear_money)
    TextView tv_halfyear_money;//半年钱
    @Bind(R.id.rl_year)
    RelativeLayout rl_year;//当年
    @Bind(R.id.tv_year_money)
    TextView tv_year_money;//当年钱

    /**
     * 根据类型获取  1寿险 2车险 3非寿不含车险
     *
     * @param type
     * @return
     */
    public AchievementSelectFragment setType(int type) {
        this.type = type;
        return this;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_achievement_select;
    }

    @Override
    protected void initView() {
        rl_month.setOnClickListener(this);
        rl_quarter.setOnClickListener(this);
        rl_halfyear.setOnClickListener(this);
        rl_year.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if(data==null) return;
        Intent in = new Intent();
        switch (view.getId()) {
            case R.id.rl_year://当年点击
                in.setClass(getContext(), WebActivity.class);
                in.putExtra("url", URL.getUrl().WEBACHIEVEMENTDETAIL + data.getId() + "/" + 4);
                startActivity(in);
                break;
            case R.id.rl_quarter://当季点击
                in.setClass(getContext(), WebActivity.class);
                in.putExtra("url", URL.getUrl().WEBACHIEVEMENTDETAIL + data.getId() + "/" + 2);
                startActivity(in);
                break;
            case R.id.rl_month://当月点击
                in.setClass(getContext(), WebActivity.class);
                in.putExtra("url", URL.getUrl().WEBACHIEVEMENTDETAIL + data.getId() + "/" + 1);
                startActivity(in);
                break;
            case R.id.rl_halfyear://半年点击
                in.setClass(getContext(), WebActivity.class);
                in.putExtra("url", URL.getUrl().WEBACHIEVEMENTDETAIL + data.getId() + "/" + 3);
                startActivity(in);
                break;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            getData();
        }
    }

    /**
     * 绑定数据
     */
    private void bindData() {
        if (data==null) return;
        tv_month_money.setText(data.getMon_achieve());
        tv_quarter_money.setText(data.getQuar_achieve());
        tv_halfyear_money.setText(data.getHalf_achieve());
        tv_year_money.setText(data.getYear_achieve());
    }

    /**
     * 获取数据
     */
    private void getData() {
        OkGo.<String>post(URL.getUrl().GETACHIEVEMENT)
                .params("m_number", MyApplication.getInstance().getUserId())
                .params("type", type)
                .execute(new MyStringCallBack<AchievementBean>(null) {
                    @Override
                    public void onSuccessData(AchievementBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getCode() == 1) {
                            data = bean.getData();
                            bindData();
                        } else {
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                    }
                });
    }
}
